

{include file='common2/header.html'}
<!-- 内容主体区域 -->
<div class="layui-body">
  <!--        <div style="padding: 15px;"><h1 style="font-weight: 700">欢迎界面</h1></div>-->
  <div id="user_container" style="padding: 15px;">
    <!--            面包屑导航-->
    <span class="layui-breadcrumb">
              <a href="#" style="font-weight: 700;color: #000!important;">首页</a>
              <a href="#" style="font-weight: 700;color: #000!important;">图书展示</a>
              <a href="" >图书列表</a>
            </span>
    <div class="layui-row layui-col-space15" style="margin-top: 15px">
      <div class="layui-col-md12">
        <div class="layui-panel" style="padding: 20px">
          <div class="layui-form-item">
            <div class="layui-input-block">
              <input id="searchVal" style="float: left" type="text" placeholder="请输入编号/名称/作者查找" autocomplete="off" class="layui-input">
              <button id="search" style="float: left;margin-left: -1px;outline: none;" type="button" class="layui-btn  "><i class="layui-icon layui-icon-search"></i></button>

              <!--                            <select name="city" lay-verify="" style="margin-left: 15px;height: 38px;border-color: #009688;">-->
              <!--                                <option value="">请选择一个类别</option>-->
              <!--                                <option value="010">北京</option>-->
              <!--                                <option value="021">上海</option>-->
              <!--                                <option value="0571">杭州</option>-->
              <!--                            </select>-->

            </div>
          </div>
          <table lay-filter="adminTable"  class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        </div>
      </div>
    </div>
  </div>
</div>
</div>







<script>
  document.querySelector('.layui-nav-itemed').classList.remove('layui-nav-itemed');
  $('a[href$=lendinghistory]').parent()[0].classList.add('layui-this');
  $('a[href$=lendinghistory]').parent().parent().parent()[0].classList.add('layui-nav-itemed');

  var formhorizontal  = document.querySelector('.form-horizontal')
  var sureBtn = document.getElementById('sureBtn');
  layui.use(['form', 'table'], function () {
    var $ = layui.jquery,
            form = layui.form,
            table = layui.table;


    // 顶部搜索逻辑
    var search = document.getElementById('search');
    var searchVal = document.getElementById('searchVal');

    search.addEventListener('click',function () {
      // console.log(searchVal.value)
      table.reload('adminTable', {
        where: { //设定异步数据接口的额外参数，任意设
          name: searchVal.value
        }
        ,page: {
          curr: 1 //重新从第 1 页开始
        }
      });
    })




    // 渲染表格
    table.render({
      elem: '#currentTableId',
      limit:4
      ,url: "{:url('/home/lendinghistory/article')}" //数据接口
      ,page: true //开启分页
      ,limits:[2,4,6,8]
      ,skin: 'row,line'
      ,id:'bookTable'
      ,cols: [[ //表头
        // {type: "checkbox", },
        {field: 'id',align:"center", width: 80, title: '编号', sort: true},
        {field: 'bname',align:"center", title: '名称'},
        {field: 'author',align:"center", title: '作者'},
        {field: 'lending_date',align:"center", title: '借阅时间',templet:function (d) {
            return formatDate(d.lending_date)
          }},
        {field: 'return_at',align:"center", title: '归还时间',templet:function (d) {
            return formatDate(d.return_at)
          }},
        {field: 'money_fine',align:"center", title: '超时金额'},

      ]],

    });


    function formatDate (value) {
      if (typeof (value) == 'undefined') {
        return ''
      } else {
        let date = new Date(parseInt(value))
        let y = date.getFullYear()
        let MM = date.getMonth() + 1
        MM = MM < 10 ? ('0' + MM) : MM
        let d = date.getDate()
        d = d < 10 ? ('0' + d) : d
        let h = date.getHours()
        h = h < 10 ? ('0' + h) : h
        let m = date.getMinutes()
        m = m < 10 ? ('0' + m) : m
        let s = date.getSeconds()
        s = s < 10 ? ('0' + s) : s
        return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
      }
    }




    function toNextTimes(n){
      let timestamp = +new Date() + n * 86400000;
      return timestamp;
    }

    // 编辑删除绑定事件
    table.on('tool(adminTable)', async function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
      var data = obj.data; //获得当前行数据
      var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
      var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
      if(layEvent === 'edit'){
        const {data:res} = await axios.post('/home/booklist/borrow',{
          id:data.id,
          lending_data:(new Date()).valueOf(),
          should_return_data:toNextTimes(3)
        })
        // console.log(res)
        if(res.state!==200){
          return layer.msg(res.message, {
            icon: 0,
            time: 1000
          })}
        table.reload('bookTable', {});
        layer.msg('借阅成功', {
          icon: 1,
          time: 1500
        })
      }

    });

  });




</script>


{include file='common2/footer.html'}
